<?php
if (defined('INCLUDE')) {
  if(!defined("CMBASIC")) {define("CMBASIC", true);}
  $cmsname = "CMReams"; // CMS-Name
  $cmsversion = "1.4 alpha 1"; // CMS-Version
  $files = array("start.php", "checks.php", "licence.php", "templatechooser.php", "database.php", "admin_acount.php", "install.php", "ende.php"); // Installationsdateien

  // Pruefe Existenz der Sesssion-Funktion
  if (!function_exists("session_start")) {
      die("Sie k&ouml;nnen CMBasic nicht benutzen: keine Session-Funktionen vorhanden ...");
  }
  
  // Session starten
  session_start();
  
  // Titel-Funktion fuer einfache Content-Ausgabe (mit Formular)
  function title($t, $overwrite = false)
  {
      global $title;
      global $content;
      global $actstep;
      if ($overwrite) {
          $title = "";
          $content = "<h1>";
      }
      $title .= $t;
      $content .= $t . "</h1>\n<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n";
  }
  
  // Ein Button wird eingefuegt
  function button()
  {
      global $content;
      $content .= "<br />\n<br />\n<button name=\"nextInstallStep\" type=\"submit\"><img src=\"setup/next.png\" alt=\"next\" title=\"next\" /> Weiter</button>\n";
  }
  // Voreinstellungen
  $title = "Setup von " . $cmsname . " " . $cmsversion . " - ";
  $content = "<h1>Setup von " . $cmsname . " " . $cmsversion . " - ";
  $actstep = 0;
  if (isset($_POST['lastStep']) && is_numeric($_POST['lastStep'])) {
      $actstep = $_POST['lastStep'] + 1;
  }
  if (isset($_GET['lastStep']) && is_numeric($_GET['lastStep'])) {
      $actstep = $_GET['lastStep'] + 1;
  }

  $menutitles = array();
  $cse = array();
  $checkfailed = false;
  foreach($files as $f => $file) {
    $execute = false;
    $executecheck = false;
    $menuitem = "";
    $settings = array();
    if ($f == $actstep-1) {
      $executecheck = true;
    }

    if ($checkfailed == false) {
      if ($f == $actstep) {
        $execute = true;
      }
    }

    if (file_exists("setup/" . $file)) {
      include_once($file);
      if (!empty($menuitem)) {
        $menutitles[] = $menuitem;
      }
      if (count($settings) > 0) {
        foreach($settings as $n => $s) {
          $cse[$n] = $s;
        }
      }
    } else {
      die("File not found: setup/" . $file);
    }
  }

  if ($actstep == (count($files)-1)) {
    $con = array("<?php");
    foreach($cse as $n => $s) {
      $con[] = str_replace("{1}", $_SESSION[$n], $s);
    }
    $fp = fopen(dirname(__FILE__) . "/../edit/config.inc.php", "w+");
    foreach($con as $c) {
      fputs($fp, $c . "\n");
    }
    if ($_SESSION['setupok'] == false) {
      fputs($fp, "define('SETUP', false);\n");
    } else {
      fputs($fp, "define('SETUP', true);\n");
    }
    fputs($fp, "?>\n");
    fclose($fp);
  }

  $c = $actstep;
  if ($checkfailed) {
      $c = $actstep-1;
  }
  $content .= "\n<input type=\"hidden\" name=\"lastStep\" value=\"" . $c . "\" />\n";
  $content .= "</form>\n";
  // Erstelle Men&uuml;
  $menu = "<ul>";
  for($i = 0; $i < count($menutitles); $i++) {
    $j = $i + 1;
    $highlight = false;
    if ($checkfailed && $i == $actstep-1) {
      $highlight = true;
    }
    if (!$checkfailed && $i == $actstep) {
      $highlight = true;
    }
    if ($highlight) {
      $menu .= "<li><span class=\"obermenuhigh\">" . $j . " | " . $menutitles[$i] . "</span></li>";
    } else {
      $menu .= "<li><a href=\"#\" title=\"" . $menutitles[$i] . "\">" . $j . " | " . $menutitles[$i] . "</a></li>";
    }
  }
  $menu .= "</ul>";
  if ($actstep == count($files) && !$checkfailed) {
    header('location: ./?id=home');
  }
  $cssfolder = "new_green";
  if (isset($_SESSION['template'])) {
    $cssfolder = $_SESSION['template'];
  }
  if(!file_exists(dirname(__FILE__) . "/../css/" . $cssfolder . "/index.php.tpl")) {
    if(!file_exists(dirname(__FILE__) . "/../css/" . "standard" . "/index.php.tpl")) {
      $hd = opendir(dirname(__FILE__)."/../css/");
      while($f = @readdir($hd)) {
        if($file!="." && $file!=".." && is_dir(dirname(__FILE__) . "/../css/".$f."/")) {
          $cssfolder = $file;
        }
      }
      closedir($hd);
    } else {
      $cssfolder = "standard";
    }
  }
  $path = "";
  $felog = "";
  $topmenu = "";
  include_once(dirname(__FILE__) . "/../css/" . $cssfolder . "/index.php.tpl");
} else {
    header('location: ../setup.php');
}

?>
